﻿using BCCommon;
using BCEntity.TMS.PlatformManageFee.PlatformManageFeeExecutionPlan;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;

namespace BCData.TMS.PlatformManageFee.PlatformManageFeeExecutionPlan
{
    public class PlatformManageFeeExecutionPlanData : IPlatformManageFeeExecutionPlanData
    {
        private readonly IDatabaseContext database;

        private readonly string QUERY = @"SELECT PlatformManageFeeExecutionPlanId,AgentRecordId,CompanyId,SubAgentRecordId,SubCompanyId,
                                        ManageFeeBusinessType,ManageFeeRatio,ExecutionType,ExecutionDate,IsDone,
                                        CreateTime,CreateAdminId,CreateAdminName 
                                        FROM PlatformManageFeeExecutionPlan";

        public PlatformManageFeeExecutionPlanData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<IEnumerable<PlatformManageFeeExecutionPlanEntity>> GetNotDoneList()
        {
            return await database.Slave.QueryAsync<PlatformManageFeeExecutionPlanEntity>($"{QUERY} WHERE IsDone = 0");
        }

        public async Task<int> UpdateIsDone(long platformManageFeeExecutionPlanId)
        {
            DynamicParameters parameters = new DynamicParameters();
            string sql = "UPDATE PlatformManageFeeExecutionPlan SET ExecutionDate = ?ExecutionDate,IsDone = ?IsDone WHERE PlatformManageFeeExecutionPlanId = ?PlatformManageFeeExecutionPlanId";
            parameters.Add("?ExecutionDate", DateTime.Now.Date, DbType.Date);
            parameters.Add("?IsDone", true, DbType.Boolean);
            parameters.Add("?PlatformManageFeeExecutionPlanId", platformManageFeeExecutionPlanId, DbType.Int64);
            return await database.Master.ExecuteAsync(sql, parameters, database.Transaction);
        }
    }
}
